﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Threading;

namespace WeatherBar2
{
    public partial class USLocation : Form
    {
        string stateAbr;
        List<string> cities = new List<string>();

        public USLocation()
        {
            InitializeComponent();
            RefreshRunner.RunWorkerCompleted += new RunWorkerCompletedEventHandler(RefreshRunner_RunWorkerCompleted);
        }

        void RefreshRunner_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            loadingPic.Visible = false;
            cityList.Items.Clear();
            foreach (string city in cities)
                cityList.Items.Add(city);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            XmlDocument stateDoc = new XmlDocument();
            stateDoc.Load(Path.GetDirectoryName(Application.ExecutablePath) + "\\states.xml");

            if (RefreshRunner.IsBusy)
            {
                RefreshRunner.CancelAsync();

                foreach (XmlNode node in stateDoc.SelectNodes("/states/state"))
                {
                    if (node.SelectSingleNode("name").InnerText == stateList.SelectedItem.ToString())
                    {
                        stateAbr = node.SelectSingleNode("ab").InnerText;
                        loadingPic.Visible = true;
                        while (RefreshRunner.IsBusy)
                        {
                            Application.DoEvents();
                        }
                        RefreshRunner.RunWorkerAsync();
                        break;
                    }
                }
            }
            else
            {
                foreach (XmlNode node in stateDoc.SelectNodes("/states/state"))
                {
                    if (node.SelectSingleNode("name").InnerText == stateList.SelectedItem.ToString())
                    {
                        stateAbr = node.SelectSingleNode("ab").InnerText;
                        loadingPic.Visible = true;
                        RefreshRunner.RunWorkerAsync();

                        break;
                    }
                }
            }
        }

        private void RefreshRunner_DoWork(object sender, DoWorkEventArgs e)
        {
            cities.Clear();
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load(string.Format("http://www.webservicex.net/uszip.asmx/GetInfoByState?USState={0}", stateAbr));
                foreach (XmlNode node in doc.SelectSingleNode("NewDataSet"))
                {
                    XmlNode childNode = node.SelectSingleNode("CITY");
                    string city = childNode.InnerText;
                    if (!cities.Contains(city))
                    {
                        cities.Add(city);
                    }
                }
            }
            catch
            {
                MessageBox.Show("Error fetching data from server.", "WeatherBar 2", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void cityList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cityList.SelectedItem != null)
            {
                lblSelection.Text = string.Format("{0}, {1}", cityList.SelectedItem.ToString(), stateAbr);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(lblSelection.Text))
            {
                ((ChangeLocation)this.Owner).txtLocation.Text = lblSelection.Text;
                this.Close();
            }
        }

    }
}
